package com.zhuiyun.project.api.videosorting.service.impl;

import com.zhuiyun.project.api.file.entity.FileManage;
import com.zhuiyun.project.api.file.service.FileManageService;
import com.zhuiyun.project.api.imagesorting.entity.ImageSorting;
import com.zhuiyun.project.api.imagesorting.mapper.ImageSortingMapper;
import com.zhuiyun.project.api.videosorting.entity.VideoSorting;
import com.zhuiyun.project.api.videosorting.mapper.VideoSortingMapper;
import com.zhuiyun.project.api.videosorting.service.VideoSortingService;
import com.zhuiyun.project.common.errorCode.CommonException;
import com.zhuiyun.project.common.response.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @ClassName VideoSortingServiceImpl
 * @Description 视频排序service实现类
 * @Author zsc
 * @Date 2024/12/9 19:26
 */
@Service
public class VideoSortingServiceImpl implements VideoSortingService {

    @Autowired
    private VideoSortingMapper videoSortingMapper;

    String projectName = "other";

    @Autowired
    private FileManageService fileManageService;

    /**
    * @Author zsc
    * @Description 新增视频排序
    * @Date 2024/12/9 19:50
    * @Return int
    * @param videoSorting
    * @param img
    * @param video
    */
    @Override
    @Transactional
    public int saveVideoSorting(VideoSorting videoSorting, MultipartFile img, MultipartFile video) throws CommonException {
        if ( img != null ){
            FileManage fileManage = fileManageService.saveFileToSystem(img,projectName);
            if(fileManage.getId()>0){
                videoSorting.setImgId(fileManage.getId());
            }
        }
        if ( video != null ){
            FileManage fileManage = fileManageService.saveFileToSystem(video,projectName);
            if(fileManage.getId()>0){
                videoSorting.setVideoId(fileManage.getId());
            }
        }
        return videoSortingMapper.insert(videoSorting);
    }

    /**
    * @Author zsc
    * @Description 删除视频排序
    * @Date 2024/12/9 19:57
    * @Return int
    * @param id
    * @param imgId
    * @param videoId
    */
    @Override
    @Transactional
    public int deleteVideoSorting(int id, int imgId, int videoId) throws CommonException {
        if (imgId > 0 ){
            CommonResult result = fileManageService.deleteImageById(imgId);
        }
        if (videoId > 0 ){
            CommonResult result = fileManageService.deleteImageById(videoId);
        }
        int i = videoSortingMapper.deleteByPrimaryKey(id);
        return i ;
    }

    /**
    * @Author zsc
    * @Description 修改视频排序
    * @Date 2024/12/9 20:09
    * @Return int
    * @param videoSorting
    * @param img
    * @param video
    */
    @Override
    @Transactional
    public int updateVideoSorting(VideoSorting videoSorting, MultipartFile img, MultipartFile video) throws CommonException {
        if (img != null){
            if (videoSorting.getImgId() > 0){
                fileManageService.deleteImageById(videoSorting.getImgId());
            }
            FileManage fileManage = fileManageService.saveFileToSystem(img,projectName);
            if(fileManage.getId()>0){
                videoSorting.setImgId(fileManage.getId());
            }
        }
        if (video != null){
            if (videoSorting.getVideoId() > 0){
                fileManageService.deleteImageById(videoSorting.getVideoId());
            }
            FileManage fileManage = fileManageService.saveFileToSystem(video,projectName);
            if(fileManage.getId()>0){
                videoSorting.setVideoId(fileManage.getId());
            }
        }
        int i = videoSortingMapper.updateByPrimaryKey(videoSorting);
        return i;
    }

    /**
    * @Author zsc
    * @Description 查询视频排序
    * @Date 2024/12/9 20:10
    * @Return java.util.List<com.zhuiyun.project.api.videosorting.entity.VideoSorting>
    */
    @Override
    public List<VideoSorting> getVideoSortingList() {
        List<VideoSorting> lists= videoSortingMapper.selectAll();
        return lists;
    }
}
